package visualcbir.decorators;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Image;
import java.net.URL;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.Player;
import javax.media.RealizeCompleteEvent;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import visualcbir.util.VSUtils;

/**
 *
 * @author sheila
 */

public class FrmVideoBox extends JFrame implements ControllerListener {

    private Component visualComponent;
    private Player player;
    ImageIcon oVideoIcon=new ImageIcon();
    JPanel pnlSummary=new JPanel();
    JPanel pnlFrames=new JPanel();
    
    public FrmVideoBox() {
        initComponents();
    }

    public void initComponents(){
        Image img=VSUtils.readImage("/home/sheila/NetBeansProjects/clusteringset-video/videoplayer/video1.png");
        oVideoIcon.setImage( img ) ;
        JLabel lblVideo = new JLabel( oVideoIcon );

        String path = "/home/sheila/NetBeansProjects/clusteringset-video/VideoBox/Level1/";
        //String path = "/home/sheila/NetBeansProjects/clusteringset-video/VideoBox/Level2/";
        //********************** SUMMARY *****path + "Summary/"**********************************
        String relativepath = path + "Summary/";
        String[] aSummary = VSUtils.getFilesByRegex(relativepath, "Frame[0-9]+\\.jpeg");
        aSummary = VSUtils.orderNumericStrings(aSummary,"Frame");
//        for (int i = 0; i < aSummary.length; i++) {
//            String string = aSummary[i];
//            System.out.print(" - "+string);
//
//        }

        for (int i = 1; i < aSummary.length; i++) {
            pnlSummary.add(new JLabel(new ImageIcon(VSUtils.setImageScaleFactor(  VSUtils.readImage(relativepath + aSummary[i])  ,  0.6 )  )));
        }
        int selectedpart = 2;
        //int selectedpart = 5;
        ((JLabel)pnlSummary.getComponent(selectedpart)).setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.red, Color.red));//createEmptyBorder(5, 5, 5, 5)

        //********************** SUMMARY ***************************************
        relativepath = path + "Frames/Segment4/";
        //relativepath = path + "Frames/Segment8/";
        String[] aFrames = VSUtils.getFilesByRegex( relativepath  , "Frame[0-9]+\\.jpeg");
        for (int i = 0; i < aFrames.length; i++) {
            pnlFrames.add(new JLabel(new ImageIcon(VSUtils.setImageScaleFactor(  VSUtils.readImage(relativepath + aFrames[i])  ,  0.4 )  )));
        }

        this.setSize(oVideoIcon.getIconWidth()+20, 600);
        this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        lblVideo.setAlignmentX(Component.CENTER_ALIGNMENT);
        //this.setLayout(new BorderLayout());


        String pathvideobox= "/home/sheila/NetBeansProjects/clusteringset-video/videoplayer/";
        JPanel pnlTempSummary =new JPanel();        
        pnlTempSummary.setLayout(new BorderLayout());
        pnlTempSummary.add(new JLabel(new ImageIcon(pathvideobox+"left.png")) , BorderLayout.WEST);
        pnlTempSummary.add(new JLabel(new ImageIcon(pathvideobox+"right.png")) , BorderLayout.EAST);
        pnlTempSummary.add( pnlSummary , BorderLayout.CENTER);

        JPanel pnlTempFrames =new JPanel();
        pnlTempFrames.setLayout(new BorderLayout());
        pnlTempFrames.add(new JLabel(new ImageIcon(pathvideobox+"leftsmall.png")) , BorderLayout.WEST);
        pnlTempFrames.add(new JLabel(new ImageIcon(pathvideobox+"rightsmall.png")) , BorderLayout.EAST);
        pnlTempFrames.add( pnlFrames , BorderLayout.CENTER);


        this.add(new JLabel(" "));
        this.add(lblVideo);
        this.add(pnlTempSummary);
        this.add(pnlTempFrames);        
        //this.dlg.add(pnl);
        
        


        try{
            player = Manager.createPlayer(new URL("file:/home/sheila/NetBeansProjects/clusteringset-video/VideoBox/Level1/Video/v59.mpg"));
            player.addControllerListener(this);
            // player.realize();
            player.start();
        }
         catch(Exception e){       e.printStackTrace();        }
    }
     @Override
   public void controllerUpdate(ControllerEvent c)
    {
            if(player == null)
                    return;

            if(c instanceof RealizeCompleteEvent)
            {
                    if((visualComponent = player.getVisualComponent()) != null)
                            this.add(visualComponent);
            }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmVideoBox().setVisible(true);
            }
        });
    }

}
